package com.fll.fangyiuser.service.impl;

import com.fll.fangyicommon.entity.user.User;
import com.fll.fangyiuser.dao.UserDao;
import com.fll.fangyiuser.service.FriendService;
import com.fll.fangyiuser.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author fll
 * create 2023/2/25
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private FriendService friendService;

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public Integer update(User user) {
        //返回更新后的用户
        Integer num = userDao.update(user);
        if (num != 0) {
            log.info("更新用户成功,记录数===>{}", num);
            return num;
        }
        log.error("更新用户失败,记录数===>{}", num);
        return null;
    }

    /**
     * 根据用户id获取用户信息
     * @param userId
     * @return
     */
    @Override
    public User getUserInfoByUserId(Integer userId) {
        User user = userDao.getUserInfoByUserId(userId);
        if (user != null){
            log.info("查询用户成功`===>{}", user);
            //赋值添加的好友数
            user.setFriendCount(friendService.getFriendCount(userId));
            return user;
        }
        log.error("查询用户失败`,没有这个用户===>{}", user);
        return null;
    }


    /**
     * 根据userId查询user信息和oss对应信息
     * @param userId
     * @return
     */
    @Override
    public User getUserAndOssByUserId(Integer userId){
        User user = userDao.getUserAndOssByUserId(userId);
        if (user != null){
            return user;
        }
        return null;
    }
}
